/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.allword.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.allword.domain.StuWorkall;
import me.zhengjie.modules.allword.repository.StuWorkallRepository;
import me.zhengjie.modules.allword.service.StuWorkallService;
import me.zhengjie.modules.allword.service.dto.StuWorkallQueryCriteria;
import me.zhengjie.modules.reply.domain.ReplyCount;
import me.zhengjie.modules.reply.repository.ReplyCountRepository;

import me.zhengjie.modules.stuwork.domain.Stuwork;
import me.zhengjie.modules.stuwork.repository.StuworkRepository;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.modules.allword.service.dto.StuWorkallDto;

/**
* @website https://eladmin.vip
* @author wzy
* @date 2024-12-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "alto管理")
@RequestMapping("/api/stuWorkall")
public class StuWorkallController {

    private final StuWorkallService stuWorkallService;
    private final StuWorkallRepository stuworkallRepository;
    private final StuworkRepository stuworkRepository;
    @PostMapping("/class/allwork")
    @AnonymousAccess
    public ResponseEntity<Object> add1ReplyCount() {
        System.out.println("掉到了");
        List<StuWorkall> stuworkList=stuworkallRepository.findAll();
        for (StuWorkall data : stuworkList) {
            int sum=0;
            List<Stuwork> stuwork=stuworkRepository.findByStuid(data.getStuid());
            for (Stuwork data2 : stuwork){
                System.out.println(data2);
                sum+=data2.getScore();
            }
            data.setScore(sum);
            stuworkallRepository.save(data);
        }
        return ResponseEntity.ok("更新成功");
    }
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('stuWorkall:list')")
    public void exportStuWorkall(HttpServletResponse response, StuWorkallQueryCriteria criteria) throws IOException {

        stuWorkallService.download(stuWorkallService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询alto")
    @ApiOperation("查询alto")
    @PreAuthorize("@el.check('stuWorkall:list')")
    public ResponseEntity<PageResult<StuWorkallDto>> queryStuWorkall(StuWorkallQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(stuWorkallService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增alto")
    @ApiOperation("新增alto")
    @PreAuthorize("@el.check('stuWorkall:add')")
    public ResponseEntity<Object> createStuWorkall(@Validated @RequestBody StuWorkall resources){
        stuWorkallService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改alto")
    @ApiOperation("修改alto")
    @PreAuthorize("@el.check('stuWorkall:edit')")
    public ResponseEntity<Object> updateStuWorkall(@Validated @RequestBody StuWorkall resources){
        stuWorkallService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除alto")
    @ApiOperation("删除alto")
    @PreAuthorize("@el.check('stuWorkall:del')")
    public ResponseEntity<Object> deleteStuWorkall(@RequestBody Integer[] ids) {
        stuWorkallService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
